package com.ruoyi.chat.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.chat.constant.RagFlowUrl;
import com.ruoyi.chat.service.IDatasetService;
import com.ruoyi.chat.request.chat.CompletionRequest;
import com.ruoyi.chat.service.impl.ChatService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * 聊天请求处理
 */
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ChatService chatService;

    @Autowired
    private IDatasetService datasetService;

    @ApiOperation("测试调用ragFlow")
    @PostMapping(value = "/{chat_id}/completions", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter complete(@Validated @RequestBody CompletionRequest completionRequest, @PathVariable("chat_id") String chatId) {
        String apiPath = RagFlowUrl.COMPLETIONS.replace("{chat_id}", chatId);
        return chatService.postRagFlow(apiPath
                , JSONObject.from(completionRequest));
    }

}
